import gulp from 'gulp';
import run from 'gulp-run';
import babel from 'gulp-babel';
import sourcemaps from 'gulp-sourcemaps';
import runSequence from 'run-sequence';

const paths = {
    src: 'src',
    build: 'build',
    dest: 'dist',
};

gulp.task('build', () => {
    return runSequence('lint',
                ['build-debug', 'build-online']);
});

gulp.task('build-debug', () => {
    return runSequence('build-clean-debug',
                'build-compile-debug');
});

gulp.task('build-online', () => {
    return runSequence('build-clean-online',
                'build-compile-online');
});

gulp.task('build-clean-online', () => {
    return run(`rm -rf ${paths.dest}`).exec();
});

gulp.task('build-clean-debug', () => {
    return run(`rm -rf ${paths.build}`).exec();
});

gulp.task('build-compile-online', () => {
    return gulp.src(`${paths.src}/**/*.js`)
        .pipe(babel())
        .pipe(gulp.dest(paths.dest));
});

gulp.task('build-compile-debug', () => {
    return gulp.src(`${paths.src}/**/*.js`)
        .pipe(sourcemaps.init())
        .pipe(babel())
        .pipe(sourcemaps.write('.', { includeContent: false, sourceRoot: '../src' }))
        .pipe(gulp.dest(paths.build));
});

gulp.task('lint', () => {
    return run(`eslint ${paths.src}`).exec();
});
